package hms.FrontDeskModule;

import hms.payment.*;
import hms.reservations.ReservationView;
import hms.rooms.Room;

import javax.swing.JOptionPane;
import java.awt.event.ActionEvent;
import hms.generic.AbstractGlobalView;
import hms.master.*;
import hms.ReservationsModule.Guest;
import hms.database.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.AbstractListModel;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.FlowLayout;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import java.awt.ComponentOrientation;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.ActionListener;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.ListModel;

public class CheckOutView extends AbstractGlobalView {

	private JPanel contentPane;
	private JList GuestList;
	private JList RoomList;
	private JList serviceList;
	private JButton guestSearchButton;
	private JButton roomSearchButton;
	private JButton payMethodButton;
	private JButton checkOutButton;
	private JTextArea GuestInfoTextArea;
	private JScrollPane scrollPane_2;
	private JScrollPane scrollPane_3;
	private JScrollPane RLScrollPane;
	private JScrollPane GLScrollPane;
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JPanel panel_4;
	private JTextField display;
	private Vector<String> guestNameVector = new Vector<String>();
	private Vector<String> roomLabelVector = new Vector<String>();
	// private Vector<String> chargerdServiceVector = new Vector<String>();
	private int guestID;
	private int room_ID;
	private String paymentType;
	private InvoiceDialog dialog;
	private JLabel lblYourSelection;
	private JTextField guestName;

	/**
	 * Create the frame.
	 */
	public CheckOutView() {
		initComponent();
		super.update(contentPane);
		super.display();
	}

	private void initComponent() {
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "Check Out",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));

		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Guest", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));

		panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, "Room", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));

		RLScrollPane = new JScrollPane();

		RoomList = new JList();
		RoomList.setFont(new Font("MS Reference Sans Serif", Font.BOLD, 14));
		RoomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		RoomList.setModel(new AbstractListModel() {
			String[] values = new String[] { "RM-1001", "room2", "room3" };

			public int getSize() {
				return values.length;
			}

			public Object getElementAt(int index) {
				return values[index];
			}
		});

		roomLabelVector = disRoomLabel(); // Fetch room label data from the DB
											// and display it using this list
		RoomList.setListData(roomLabelVector);// Initialize the list with the
												// data from the DB

		RLScrollPane.setViewportView(RoomList);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

		GLScrollPane = new JScrollPane();

		GuestList = new JList();
		GuestList.setFont(new Font("MS Reference Sans Serif", Font.BOLD, 14));
		/*
		 * GuestList.addListSelectionListener(new ListSelectionListener() {
		 * public void valueChanged(ListSelectionEvent e) { if
		 * (e.getValueIsAdjusting() == false) {
		 * 
		 * if (GuestList.getSelectedIndex() == -1) { //No selection, disable
		 * fire button. guestSearchButton.setEnabled(false);
		 * 
		 * } else { //Selection, enable the fire button.
		 * guestSearchButton.setEnabled(true); } } } });
		 */
		GuestList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		GuestList.setModel(new AbstractListModel() {
			String[] values = new String[] { "YIFENG ZOU", "guest2", "guest3" };

			public int getSize() {
				return values.length;
			}

			public Object getElementAt(int index) {
				return values[index];
			}
		});

		guestNameVector = disGuestName(); // Fetch guest name data from the DB
											// and display it using this list
		GuestList.setListData(guestNameVector);// Initialize the list with the
												// data from the DB

		GLScrollPane.setViewportView(GuestList);
		panel.add(GLScrollPane);

		panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(null, "Guest Information",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));

		panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(null, "Service Charged",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));

		panel_4 = new JPanel();

		display = new JTextField();
		display.setEditable(false);
		display.setColumns(10);

		lblYourSelection = new JLabel("Your Selection:");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				panel_4,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGap(33)
																		.addComponent(
																				panel,
																				GroupLayout.DEFAULT_SIZE,
																				99,
																				Short.MAX_VALUE)
																		.addGap(24)
																		.addComponent(
																				panel_2,
																				GroupLayout.DEFAULT_SIZE,
																				99,
																				Short.MAX_VALUE)
																		.addGap(28)
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								panel_1,
																								GroupLayout.DEFAULT_SIZE,
																								398,
																								Short.MAX_VALUE)
																						.addComponent(
																								panel_3,
																								GroupLayout.DEFAULT_SIZE,
																								398,
																								Short.MAX_VALUE)))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				lblYourSelection)
																		.addGap(18)
																		.addComponent(
																				display,
																				GroupLayout.PREFERRED_SIZE,
																				227,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGap(10)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addComponent(
																				panel_1,
																				GroupLayout.DEFAULT_SIZE,
																				124,
																				Short.MAX_VALUE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				panel_3,
																				GroupLayout.DEFAULT_SIZE,
																				124,
																				Short.MAX_VALUE))
														.addComponent(
																panel_2,
																GroupLayout.DEFAULT_SIZE,
																254,
																Short.MAX_VALUE)
														.addComponent(
																panel,
																GroupLayout.DEFAULT_SIZE,
																254,
																Short.MAX_VALUE))
										.addGap(18)
										.addComponent(panel_4,
												GroupLayout.DEFAULT_SIZE, 48,
												Short.MAX_VALUE)
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblYourSelection)
														.addComponent(
																display,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(7)));

		guestSearchButton = new JButton("Guest Search");
		guestSearchButton.setToolTipText("Please select a guest in the list");

		// default select the first item
		GuestList.setSelectedIndex(0);

		guestSearchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				guestSearch();
			}
		});
		guestSearchButton.setActionCommand("guestSearchButton");

		roomSearchButton = new JButton("Room Search");
		roomSearchButton.setToolTipText("Please select a room in the list");
		// default select the first item
		RoomList.setSelectedIndex(0);
		roomSearchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				roomSearch();
			}
		});
		roomSearchButton.setActionCommand("roomSearchButton");

		payMethodButton = new JButton("Pay Method");
		payMethodButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectPayMethod();
			}
		});
		payMethodButton.setActionCommand("payMethodButton");

		checkOutButton = new JButton("Check Out");
		checkOutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				checkOut();
			}
		});
		checkOutButton.setActionCommand("checkOutButton");

		guestName = new JTextField();
		guestName.setToolTipText("Please enter: first_name+space+last_name");
		guestName.setColumns(10);

		JLabel lblEnterCheckoutGuest = new JLabel("Enter Check-Out Guest Name:");

		GroupLayout gl_panel_4 = new GroupLayout(panel_4);
		gl_panel_4
				.setHorizontalGroup(gl_panel_4
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								Alignment.TRAILING,
								gl_panel_4
										.createSequentialGroup()
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																gl_panel_4
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				checkOutButton))
														.addGroup(
																gl_panel_4
																		.createSequentialGroup()
																		.addGap(19)
																		.addComponent(
																				guestSearchButton)
																		.addGap(18)
																		.addComponent(
																				roomSearchButton,
																				GroupLayout.PREFERRED_SIZE,
																				105,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				119,
																				Short.MAX_VALUE)
																		.addComponent(
																				payMethodButton,
																				GroupLayout.PREFERRED_SIZE,
																				105,
																				GroupLayout.PREFERRED_SIZE)
																		.addGap(18)
																		.addComponent(
																				lblEnterCheckoutGuest)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				guestName,
																				GroupLayout.PREFERRED_SIZE,
																				79,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		gl_panel_4
				.setVerticalGroup(gl_panel_4
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_4
										.createSequentialGroup()
										.addGroup(
												gl_panel_4
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_panel_4
																		.createParallelGroup(
																				Alignment.BASELINE,
																				false)
																		.addComponent(
																				guestSearchButton)
																		.addComponent(
																				roomSearchButton)
																		.addComponent(
																				payMethodButton)
																		.addComponent(
																				lblEnterCheckoutGuest))
														.addGroup(
																gl_panel_4
																		.createSequentialGroup()
																		.addGap(2)
																		.addComponent(
																				guestName,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addComponent(checkOutButton)
										.addContainerGap()));
		panel_4.setLayout(gl_panel_4);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.X_AXIS));
		panel_2.add(RLScrollPane);
		panel_3.setLayout(new BorderLayout(0, 0));

		scrollPane_3 = new JScrollPane();
		panel_3.add(scrollPane_3);

		serviceList = new JList();
		serviceList.setFont(new Font("Microsoft Sans Serif", Font.BOLD, 14));
		serviceList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		// display the services in the serviceList
		// chargerdServiceVector=disServiceList();
		// serviceList.setListData(chargerdServiceVector);

		scrollPane_3.setViewportView(serviceList);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));

		scrollPane_2 = new JScrollPane();
		panel_1.add(scrollPane_2);

		GuestInfoTextArea = new JTextArea();
		GuestInfoTextArea.setFont(new Font("MS Reference Sans Serif",
				Font.BOLD, 14));
		GuestInfoTextArea.setEditable(false);
		scrollPane_2.setViewportView(GuestInfoTextArea);
		contentPane.setLayout(gl_contentPane);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equalsIgnoreCase("guestSearchButton")) {
			guestSearch();
		}
		if (e.getActionCommand().equalsIgnoreCase("roomSearchButton")) {
			roomSearch();
		}
		if (e.getActionCommand().equalsIgnoreCase("payMethodButton")) {
			selectPayMethod();
		}
		if (e.getActionCommand().equalsIgnoreCase("checkOutButton")) {
			checkOut();
		}

	}

	public void guestSearch() {

		String gFirstName;
		String gLastName;
		int gRoomID = 0;
		String gRoomLabel = "";
		String guestName = " ";
		Object name = " ";

		name = GuestList.getSelectedValue();
		guestName = name.toString();
		display.setText("You Select Guest: " + guestName);
		// System.out.println(name.toString());
		String gName[] = guestName.split(" ");
		gFirstName = gName[0];
		gLastName = gName[1];
		String address = " ";

		DAO localDAO = MasterController.getDatabaseObject();

		ResultSet rs1 = localDAO
				.getResult("Select* from GUEST where FIRST_NAME='" + gFirstName
						+ "';");
		try {
			while (rs1.next()) {
				gRoomID = rs1.getInt("ROOMID");
				room_ID = rs1.getInt("ROOMID");
				guestID = rs1.getInt("GUEST_ID"); // store the guestID which is
													// declared in the
													// CheckOutView class
				address = rs1.getString("ADDRESS") + ", "
						+ rs1.getString("CITY") + ", " + rs1.getString("STATE");
				// System.out.println("GUEST ID is: "+guestID);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		ResultSet rs2 = localDAO.getResult("Select* from ROOM where ROOMID='"
				+ gRoomID + "';");
		try {
			while (rs2.next()) {
				gRoomLabel = rs2.getObject("ROOMLABEL").toString();
				// System.out.println("Room Label is: "+gRoomLabel);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		GuestInfoTextArea.setText(""); // clear the textArea before display
		GuestInfoTextArea.append("GUEST NAME:  " + gFirstName + " " + gLastName
				+ "\n");
		GuestInfoTextArea.append("GUEST ROOM:  " + gRoomLabel + "\n");
		GuestInfoTextArea.append("GUEST ADDRESS:  " + address + "\n");

		// GuestInfoTextArea.append("TOTAL Balance:   "+balance+"\n");
		disGuestService(); // display the charged guest service

	}

	public void roomSearch() {
		String roomLabel = " ";
		String gFirstName = " ";
		String gLastName = " ";
		int roomID = 0;
		Object room = " ";

		room = RoomList.getSelectedValue();
		roomLabel = room.toString();
		// System.out.println(roomLabel);
		display.setText("You Select Room: " + roomLabel);

		DAO localDAO = MasterController.getDatabaseObject();

		ResultSet rs1 = localDAO
				.getResult("Select* from ROOM where ROOMLABEL='" + roomLabel
						+ "';");
		try {
			while (rs1.next()) {
				roomID = rs1.getInt("ROOMID");
				room_ID = rs1.getInt("ROOMID");
				// System.out.println("ROOM ID is: "+roomID);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}


		ReservationView rv = new ReservationView();
		rv.fillForRoomOnDate(new Room(room_ID), new GregorianCalendar());
		Guest guest = new Guest(rv.get(0).getPayingCustomerID());
		gFirstName = guest.getFirstName();
		gLastName = guest.getLastName();
		
		if (gFirstName == " " || gLastName == " ") {
			GuestInfoTextArea.setText("");
			GuestInfoTextArea.append("ROOM LABEL:  " + roomLabel + "\n");
			GuestInfoTextArea.append("ROOM STATUS:  " + "EMPTY" + "\n");
		} else {
			GuestInfoTextArea.setText("");
			GuestInfoTextArea.append("ROOM LABEL:  " + roomLabel + "\n");
			GuestInfoTextArea.append("GUEST NAME:  " + gFirstName + " "
					+ gLastName + "\n");
		}
		// GuestInfoTextArea.append("TOTAL Balance:   "+balance+"\n");
		disRoomService(); // display the charged room service
	}

	public void selectPayMethod() {
		String guestName = " ";
		Object name = GuestList.getSelectedValue();
		guestName = name.toString();
		String gName[] = guestName.split(" ");
		String gFirstName = gName[0];
		String gLastName = gName[1];
		int gID = -1;
		int reservationID = -1;// initial as a invalid reservation ID

		DAO localDAO = MasterController.getDatabaseObject();
		String gstSearchSQL = "Select* from GUEST where upper(FIRST_NAME)=upper('" + gFirstName
				+ "') and upper(LAST_NAME)=upper('" + gLastName + "');";
		ResultSet rs1 = localDAO
				.getResult(gstSearchSQL);
		try {
			while (rs1.next()) {
				gID = rs1.getInt("GUEST_ID"); // Get the ID of this Guest
				// System.out.println("Guest ID is: "+gID);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		ResultSet rs2 = localDAO
				.getResult("select* from RESERVATION where PAYING_CUSTOMER_ID="
						+ gID + ";");
		try {
			while (rs2.next()) {
				reservationID = rs2.getInt("RESERVATION_ID"); // Get the ID of
																// this Guest
				// System.out.println("RESERVATION ID is: "+reservationID);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		if (reservationID != (-1)) {
			// only the guest who did the reservation(means reservation!=-1) can
			// do check out
			payMethod selection = new payMethod(reservationID);
		} else
			JOptionPane.showMessageDialog(contentPane,
					"Please select the guest who did the reservation!",
					"Error", JOptionPane.ERROR_MESSAGE);

	}

	public int checkOut() {
		
		dialog = new InvoiceDialog();
		dialog.setTitle("Guest Invoice");
		// dialog.setVisible(true);

		String checkOutname = guestName.getText();
		// input error detection.
		// Pattern p=Pattern.compile(".*\\s.*");
		// Matcher m=p.matcher(checkOutname);
		// if(!m.matches()){
		if (checkOutname.trim().isEmpty()) {
			JOptionPane
					.showMessageDialog(
							contentPane,
							"Invalid Check out Guest Name! Please enter the guest who did the reservation!",
							"Error", JOptionPane.ERROR_MESSAGE);
			dialog.setVisible(false);
			dialog=null;
			return 0;
		}
		//dialog.setVisible(true);
		// System.out.println(checkOutname);
		String gName[] = checkOutname.split(" ");
		String gFirstName = gName[0];
		String gLastName = gName[1];
		// int ID=-1;

		gFirstName = gFirstName.toUpperCase();
		gLastName = gLastName.toUpperCase();
		double balance = 0.0; // calculate the total balance
		int reservationID = -1;// initial to invalid reservation ID
		int payGuestID = -1;// initial to a invalid guest ID
		String rooms = ""; // store all the rooms reserved by one guest

		DAO localDAO = MasterController.getDatabaseObject();

		// user input validation query. if no such guest, pop up error message
		String gstSearchSQL = "select * from GUEST where upper(FIRST_NAME) =UPPER('"
				+ gFirstName + "') and upper(LAST_NAME)=upper('" + gLastName
				+ "');";
		ResultSet rs8 = localDAO
				.getResult(gstSearchSQL);
		try {
			int next = 0;

			while (rs8.next()) {
				dialog.setVisible(false);
				
				// System.out.println("invalid");
				//

				//
				next++;
			}
			if (next == 0) {
				JOptionPane
						.showMessageDialog(
								contentPane,
								"Invalid Check out Guest Name! Please enter the guest who did the reservation",
								"Error", JOptionPane.ERROR_MESSAGE);
				dialog=null;
				return 0;
			}

			// System.out.print("next "+next);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		// Query to find the guest id corresponding to the check out guest name
		// that entered by the user
		String payingGuestSrchSQL = "select* from GUEST where upper(FIRST_NAME) ='"
				+ gFirstName + "' and upper(LAST_NAME)=upper('" + gLastName + "')";
		ResultSet rs6 = localDAO
				.getResult(payingGuestSrchSQL);
		try {
			while (rs6.next()) {
				payGuestID = rs6.getInt("GUEST_ID");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		// user input validation query. if the guest didn't do the reservation,
		// pop up error message
		ResultSet rs9 = localDAO
				.getResult("select* from RESERVATION where PAYING_CUSTOMER_ID="
						+ payGuestID + ";");
		try {
			while (!rs9.next()) {
				dialog.setVisible(false);
				JOptionPane
						.showMessageDialog(contentPane,
								"Invalid Check out Guest Name!Please enter the guest who did the reservation");
				dialog=null;
				return 0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		// Query to find the reservation id corresponding to the check out guest
		ResultSet rs7 = localDAO
				.getResult("select* from RESERVATION where PAYING_CUSTOMER_ID="
						+ payGuestID + ";");
		try {
			while (rs7.next()) {
				reservationID = rs7.getInt("RESERVATION_ID");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		dialog.getNameField().setText(gFirstName + " " + gLastName);
		/*
		 * //Query to find out the guest who did the reservation ResultSet rs =
		 * localDAO
		 * .getResult("select* from RESERVATION where RESERVATION_ID="+reservationID
		 * +";"); try { while(rs.next()){
		 * payGuestID=rs.getInt("PAYING_CUSTOMER_ID"); } } catch (SQLException
		 * e) { e.printStackTrace(); }
		 * 
		 * ResultSet rs1 =
		 * localDAO.getResult("select* from GUEST where GUEST_ID="
		 * +payGuestID+";"); try { while(rs1.next()){
		 * dialog.getNameField().setText
		 * (rs1.getString("FIRST_NAME")+" "+rs1.getString("LAST_NAME")); } }
		 * catch (SQLException e) { e.printStackTrace(); }
		 */
		//String rmSearch = "select ROOMLABEL from ROOM where RESERVATION_ID="
			//	+ reservationID + ";";
		String rmSearch = "select ROOMLABEL from ROOM where roomid in (select roomid from room_reservation where reservation_id="
					+ reservationID + ");";
		ResultSet rs2 = localDAO
				.getResult(rmSearch);
		try {
			while (rs2.next()) {
				rooms += rs2.getString("ROOMLABEL") + ", ";
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		dialog.getRoomField().setText(rooms);

		// Query to find all the guest services associate with a reservation ID,
		// and calculate the price
		String querySQL_1 = "select * from GUEST_SERVICE where GUEST_SERVICE_ID in("
				+ "select GUEST_SERVICE_ID from GUEST_SERVICE_RESERVATION where RESERVATION_ID="
				+ reservationID + ");";
		ResultSet rs3 = localDAO.getResult(querySQL_1);
		try {
			while (rs3.next()) {
				balance += rs3.getDouble("PRICE"); // calculate guest services
													// balance
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		// Query to find all the room services associate with a reservation ID,
		// and calculate the price
		String querySQL_2 = "select * from ROOM_SERVICE where ROOM_SERVICE_ID in("
				+ "select ROOM_SERVICE_ID from ROOM_SERVICE_RESERVATION where RESERVATION_ID="
				+ reservationID + ");";
		ResultSet rs4 = localDAO.getResult(querySQL_2);
		try {
			while (rs4.next()) {
				balance += rs4.getDouble("PRICE"); // calculate room services
													// balance
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		dialog.getAmountField().setText("$ " + balance); // display balance in
															// the invoice

		ResultSet rs5 = localDAO.getResult("select* from GUEST where GUEST_ID="
				+ payGuestID + ";");
		try {
			while (rs5.next()) {
				dialog.getAddress().setText(
						rs5.getString("ADDRESS") + ", " + rs5.getString("CITY")
								+ ", " + rs5.getString("STATE")); // display
																	// guest
																	// address
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		disServiceList(reservationID);

		dialog.setVisible(true);
		return 1;
	}

	public Vector<String> disGuestName() {

		Vector<String> guestName = new Vector<String>();
		DAO localDAO = MasterController.getDatabaseObject();
		// ResultSet rs =
		// localDAO.getResult("Select FIRST_NAME,LAST_NAME from GUEST");
		ResultSet rs = localDAO
				.getResult("Select FIRST_NAME,LAST_NAME from GUEST where GUEST_ID in (Select GUEST_ID from GUEST_RESERVATION);");
		try {

			while (rs.next()) {
				String firstName = rs.getObject("FIRST_NAME").toString();
				String lastName = rs.getObject("LAST_NAME").toString();
				String gName = firstName + " " + lastName;
				guestName.add(gName);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return guestName;
	}

	public Vector<String> disRoomLabel() {
		Vector<String> roomLabel = new Vector<String>();

		DAO localDAO = MasterController.getDatabaseObject();
		// ResultSet rs = localDAO.getResult("Select ROOMLABEL from ROOM");
		ResultSet rs = localDAO
				.getResult("Select ROOMLABEL from ROOM where ROOMID in (select ROOMID from ROOM_RESERVATION);");
		try {

			while (rs.next()) {
				roomLabel.add(rs.getObject("ROOMLABEL").toString());

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return roomLabel;
	}

	public int getReservationID() {
		int reservationID = -1; // make sure this is a invalid reservationID if
								// it is not retrieved from the DB
		DAO localDAO = MasterController.getDatabaseObject();
		ResultSet rs = localDAO
				.getResult("select* from GUEST_RESERVATION where GUEST_ID="
						+ guestID + ";");
		try {
			while (rs.next()) {
				reservationID = rs.getInt("RESERVATION_ID");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return reservationID;
	}

	// function to display all charged services and calculate total balance
	public void disServiceList(int RSID) {
		Vector<String> service = new Vector<String>();
		int reservationID = RSID;
		// System.out.println(reservationID);
		DAO localDAO = MasterController.getDatabaseObject();
		// Query to find all the ordered guest services associated with a
		// reservationID
		String querySQL_1 = "select* from GUEST_SERVICE where GUEST_SERVICE_ID in("
				+ "select GUEST_SERVICE_ID from GUEST_SERVICE_RESERVATION where RESERVATION_ID="
				+ reservationID + ");";
		ResultSet rs1 = localDAO.getResult(querySQL_1);
		try {

			while (rs1.next()) {
				String description = rs1.getString("DESCRIPTION");
				double price = rs1.getDouble("PRICE");
				// balance+=price;
				// String guestService=description+"     "+price;
				// System.out.println(guestService);
				String guestService = String.format(
						"%s:                    $%4.2f", description, price);
				service.add(guestService); // add guest service into the service
											// vector
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// Query to find all the ordered room services associated with a
		// reservationID
		String querySQL_2 = "select* from ROOM_SERVICE where ROOM_SERVICE_ID in("
				+ "select ROOM_SERVICE_ID from ROOM_SERVICE_RESERVATION where RESERVATION_ID="
				+ reservationID + ");";
		ResultSet rs2 = localDAO.getResult(querySQL_2);
		try {

			while (rs2.next()) {
				String description = rs2.getString("DESCRIPTION");
				double price = rs2.getDouble("PRICE");
				// balance+=price;
				// String roomService=description+"     "+price;
				// System.out.println(roomService);
				String roomService = String.format(
						"%s:                    $%4.2f", description, price);
				service.add(roomService); // add room service into the service
											// vector
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		serviceList.setListData(service);
	}

	public void disGuestService() {
		Vector<String> service = new Vector<String>();
		DAO localDAO = MasterController.getDatabaseObject();

		String querySQL_1 = "select* from GUEST_SERVICE where GUEST_SERVICE_ID in("
				+ "select GUEST_SERVICE_ID from GUEST_SERVICE_RESERVATION where GUEST_ID="
				+ guestID + ");";
		ResultSet rs1 = localDAO.getResult(querySQL_1);
		try {
			while (rs1.next()) {
				String description = rs1.getString("DESCRIPTION");
				double price = rs1.getDouble("PRICE");
				String guestService = String.format(
						"%s:                    $%4.2f", description, price);
				service.add(guestService); // add room service into the service
											// vector
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		serviceList.setListData(service);
	}

	public void disRoomService() {
		Vector<String> service = new Vector<String>();
		DAO localDAO = MasterController.getDatabaseObject();

		String querySQL_1 = "select* from ROOM_SERVICE where ROOM_SERVICE_ID in("
				+ "select ROOM_SERVICE_ID from ROOM_SERVICE_RESERVATION where ROOMID="
				+ room_ID + ");";
		ResultSet rs1 = localDAO.getResult(querySQL_1);
		try {
			while (rs1.next()) {
				String description = rs1.getString("DESCRIPTION");
				double price = rs1.getDouble("PRICE");
				String roomService = String.format(
						"%s:                    $%4.2f", description, price);
				service.add(roomService); // add room service into the service
											// vector
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		serviceList.setListData(service);
	}

	public InvoiceDialog getDialog() {
		return dialog;
	}
}
